<template>
  <div v-loading="loading" class="department-container">
    <div class="app-container">
      <!-- 组织架构头部 -->
      <el-card class="tree-card">
        <tree-tools :tree-node="company" :is-root="true" @addDept="addDept" />

        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <tree-tools
            slot-scope="{ data }"
            :tree-node="data"
            @addDept="addDept"
            @delDepts="getDepartments()"
            @editDept="editDept"
          />
        </el-tree>
      </el-card>
    </div>
    <!-- 弹出层 -->
    <add-dept
      ref="addDept"
      :show-dialog.sync="showDialog"
      :tree-node="node"
      @addDepts="getDepartments()"
    />
  </div>
</template>

<script>
import addDept from './components/add-dept'
import { tranListToTreeData } from '@/utils'
import { getDepartments } from '@/api/departments'
import TreeTools from './components/tree-tools'
export default {
  components: {
    TreeTools,
    addDept
  },
  data () {
    return {
      company: { name: '江西赚它一个亿科技股份有限公司 ', manager: '负责人', id: '' },
      departs: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      showDialog: false,
      node: null,
      loading: false
    }
  },

  created () {
    this.getDepartments()
  },

  methods: {
    async getDepartments () {
      this.loading = true
      const res = await getDepartments()
      // console.log(res)
      this.departs = tranListToTreeData(res.depts, '')
      this.company = { name: '江西赚它一个亿科技股份有限公司 ', manager: '负责人', id: '' }
      this.loading = false
    },

    addDept (node) {
      this.node = node
      this.showDialog = true
    },

    editDept (node) {
      this.node = node
      // 获取部门详情
      this.$refs.addDept.getDepartmentDetail(node.id)
      this.showDialog = true
    }
  }
}
</script>

<style  lang="scss" scoped>
.el-dropdown-link {
  cursor: pointer;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
